import request from '@/utils/request'
import { LogIn } from './index.d'

// 登录
export function logIn(data: LogIn) {
  return request({
    url: '/api/User/Login',
    method: 'post',
    data
  })
}
function getToken() {
  return request({
    url: '/api/User/RefreshToken',
    method: 'post',
    data: {
      token: JSON.parse(sessionStorage.getItem('USAdrop_user')).RefreshToken
    },
    headers: {
      'Content-Type': 'application/json;',
      Authorization: 'Bearer ' + sessionStorage.getItem('Authorization')
    }
  })
}
import { setToken } from '@/utils/token'
export const getNewToken = () => {
  return getToken()
    .then((res) => {
      // 重置token成功
      if (res.data.Success) {
        setToken(res.data.Result)
        return res.data.Result.Token
      } else {
        // 清除过期token
        sessionStorage.removeItem('USAdrop_user')
        sessionStorage.removeItem('Authorization')
        //   重置失败返回登录
        window.location.href = '/login'
        return Promise.reject()
      }
    })
    .catch(() => {
      // 清除过期token
      sessionStorage.removeItem('USAdrop_user')
      sessionStorage.removeItem('Authorization')
      //   重置失败返回登录
      window.location.href = '/login'
      return Promise.reject()
    })
}

//api/User/RefreshToken'
// 刷新token
export function refreshToken() {
  return request({
    url: '/api/User/RefreshToken',
    method: 'post'
  })
}
